package com.wuba.rxjavademo.lyz.day0416;

import java.util.ArrayList;

import rx.Observable;

/**
 * Created by liyaxing on 2016/4/22.
 */
public class Demo08 {

    public static void main(String[] ss){

        Demo08 demo08 = new Demo08() ;
        Observable<ArrayList<Integer>> observable = demo08.collectObservable() ;
        observable.subscribe(list -> {
            for (int i = 0; i < list.size(); i++) {
                print(list.get(i));
            }
        }) ;


    }

    private Observable<ArrayList<Integer>> collectObservable(){
        return Observable.just(1,2,3,4,5,6,7).collect(()-> new ArrayList<>(),(list, i) -> list.add(i)) ;
    }

    private Observable<Integer> reduceObservable(){
        return Observable.just(1,2,3,4,5).reduce((x,y) -> x + y) ;
    }

    private Observable<Integer> countObservable(){
        return concatObservable().count() ;
    }

    private Observable<Integer> concatObservable(){
        Observable<Integer> o1 = Observable.just(1,2,3) ;
        Observable<Integer> o2 = Observable.just(4,5,6) ;
        Observable<Integer> o3 = Observable.just(7,8,9) ;
        return Observable.concat(o1,o2,o3) ;
    }

    private static void print(Object obj){
        System.out.println(obj) ;
    }

}
